/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 版本: 苏州公安统一用户管理平台1.0
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名:SysResourceDaoImpl.java
 * <p>
 * 类产生时间: 2012-7-11 下午6:04:55
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 所在组 : 苏州公安统一用户管理平台
 * <p>
 * 所在部门: 电信/国土——技术二部
 * <p>
 * <p>
 */
package com.oceansoft.core.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.oceansoft.core.common.util.PageUtil;
import com.oceansoft.core.common.util.Pagination;
import com.oceansoft.core.dao.ISysResourceDao;
import com.oceansoft.core.dao.mapper.SysResourceMapper;
import com.oceansoft.core.domain.SysResource;

/**
 * 
 * @author chenw
 * 
 */
@Repository
public class SysResourceDaoImpl implements ISysResourceDao {
	@Resource
	private JdbcTemplate sjt;

	@Override
	public List<SysResource> findSysResourcesByUser(String userId) {
		List<SysResource> list = sjt
				.query("SELECT T3.NAME,T3.URL,T3.PARENT_ID,T3.RESID||'' RESID,T3.REMARK  FROM T_SYS_RESOURCE T3 WHERE EXISTS (SELECT T2.*, ROWNUM RUM FROM T_SYS_ROLE_RESOURCE T2 WHERE T2.ROLE_ID IN (SELECT T.ROLEID FROM T_SYS_USER_ROLE T WHERE T.USERID = ? )AND T2.RES_ID = T3.RESID) ORDER BY T3.ORDERNUM",
						new RowMapper<SysResource>() {
							@Override
							public SysResource mapRow(ResultSet rs, int index) throws SQLException {

								SysResource res = new SysResource(rs.getString("NAME"), rs.getString("URL"), rs
										.getString("PARENT_ID"));
								res.setResid(rs.getString("RESID"));
								res.setRemark(rs.getString("REMARK"));
								return res;
							}
						}, userId);
		return list;
	}

	@Override
	public Pagination<SysResource> findSysResourceByPage(int pageIndex) {
		List<SysResource> list = sjt.query(
				PageUtil.getPageSQL("SELECT * FROM T_SYS_RESOURCE ORDER BY PARENT_ID,ORDERNUM", pageIndex),
				new SysResourceMapper());
		int _total = sjt.queryForInt("SELECT COUNT(1) FROM T_SYS_RESOURCE");
		return new Pagination<SysResource>(list, _total, pageIndex);
	}

	@Override
	public Pagination<SysResource> findSysResourceByPage(String name, int pageIndex) {
		String sql = "SELECT * FROM T_SYS_RESOURCE WHERE NAME LIKE '%" + name + "%' ORDER BY ORDERNUM";
		List<SysResource> list = sjt.query(PageUtil.getPageSQL(sql, pageIndex), new SysResourceMapper());
		int _total = sjt.queryForInt(PageUtil.getCountSQL(sql));
		return new Pagination<SysResource>(list, _total, pageIndex);
	}

	@Override
	public int findRoleResourceRowsByResourceId(String resourceId) {
		String sql = "SELECT COUNT(1) FROM T_SYS_ROLE_RESOURCE T WHERE T.RES_ID = ?";
		return sjt.queryForInt(sql, resourceId);
	}

	@Override
	public void deleteResourceById(String resourceId) {
		sjt.update("DELETE FROM T_SYS_RESOURCE T WHERE T.RESID = ?", resourceId);
	}

	@Override
	public void saveSysResource(SysResource resource) {
		sjt.update(
				"INSERT INTO T_SYS_RESOURCE(DBID,RESID,NAME,URL,ORDERNUM,CDATE,REMARK,PARENT_ID) VALUES(SEQ_SYS_RESOURCE.NEXTVAL,SEQ_RESOURCE_CODE.NEXTVAL,?,?,?,?,?,?)",
				resource.getName(), resource.getUrl(), resource.getOrdernum(), resource.getCdate(),
				resource.getRemark(), resource.getParent_id());
	}

	@Override
	public SysResource findSysResourceById(String resourceId) {
		return sjt.queryForObject("SELECT * FROM T_SYS_RESOURCE WHERE RESID = ?", new SysResourceMapper(), resourceId);
	}

	@Override
	public void updateSysResource(SysResource resource) {
		sjt.update(
				"UPDATE T_SYS_RESOURCE SET NAME=?,URL=?,ORDERNUM=?,CDATE=SYSDATE,REMARK=?,PARENT_ID = ? WHERE RESID = ?",
				resource.getName(), resource.getUrl(), resource.getOrdernum(), resource.getRemark(),
				resource.getParent_id(), resource.getResid());
	}

	@Override
	public List<SysResource> findSysResourceByRoleId(String roleId) {
		String sql = "SELECT RE.* FROM T_SYS_RESOURCE RE LEFT JOIN T_SYS_ROLE_RESOURCE RR ON RR.RES_ID = RE.RESID WHERE RR.ROLE_ID = ?";
		return sjt.query(sql, new SysResourceMapper(), roleId);
	}

	@Override
	public List<SysResource> findAllResource() {
		String sql = "SELECT * FROM T_SYS_RESOURCE";
		return sjt.query(sql, new SysResourceMapper());
	}

}
